package com.edu.controller;

import com.edu.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/*
 * @Author:princess
 * @Date 2023/11/29 11 29 18:49
 *
 */
@Controller
public class UploadController {
    @RequestMapping("/upload01")
    public String upload01(@RequestParam("headImg") MultipartFile uploadFile){
        System.out.println("文件类型为："+uploadFile.getContentType());
        System.out.println("获取文件的name属性的值："+uploadFile.getName());
        System.out.println("获取原本的文件名："+uploadFile.getOriginalFilename());
        System.out.println("文件大小："+uploadFile.getSize());

        File file = new File("d:/headImg/"+uploadFile.getOriginalFilename());
        try {
            uploadFile.transferTo(file);
            System.out.println("上传成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return "index.jsp";
    }

    @RequestMapping("/upload02")
    public String upload01(User user, @RequestParam(required = false,name = "headImg") MultipartFile uploadFile){
        System.out.println(user);
        File file = new File("d:/headImg/"+uploadFile.getOriginalFilename());
        try {
            uploadFile.transferTo(file);
            System.out.println("上传成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return "index.jsp";
    }

    @RequestMapping("/upload03")
    public String upload03( MultipartFile[] headImg){
        try {
            for (MultipartFile multipartFile : headImg){
                multipartFile.transferTo(new File("d:/headImg/" + multipartFile.getOriginalFilename()));
            }
            System.out.println("上传成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return "index.jsp";
    }
}
